package com.yiqi.lottery.feature.football.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.CouprceBo;
import com.yiqi.lottery.feature.home.view.PayActivity;
import com.yiqi.lottery.feature.lottery.contract.HallContract;
import com.yiqi.lottery.feature.lottery.dependencies.hall.DaggerHallComponent;
import com.yiqi.lottery.feature.lottery.dependencies.hall.HallPresenterModule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class FootballMatchHemaiActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {

    @Inject
    HallContract.Presenter mPresenter;

    @BindView(R.id.img_back)
    ImageView img_back;
    @BindView(R.id.tv_fangan_money)
    TextView tv_fangan_money;
    @BindView(R.id.tv_per_money)
    TextView tv_per_money;
    @BindView(R.id.tv_invalid_fenshu)
    TextView tv_invalid_fenshu;
    @BindView(R.id.et_fenchengshu)
    EditText et_fenchengshu;
    @BindView(R.id.check_1)
    CheckBox check_1;
    @BindView(R.id.ll_fangan_ticheng)
    LinearLayout ll_fangan_ticheng;
    @BindView(R.id.et_rengoushu)
    EditText et_rengoushu;
    @BindView(R.id.et_baodishu)
    EditText et_baodishu;
    @BindView(R.id.tv_gongkai)
    TextView tv_gongkai;
    @BindView(R.id.tv_canyukejian)
    TextView tv_canyukejian;
    @BindView(R.id.tv_jianzhikejian)
    TextView tv_jianzhikejian;
    @BindView(R.id.et_xuanyan)
    EditText et_xuanyan;
    @BindView(R.id.et_miaoshu)
    EditText et_miaoshu;
    @BindView(R.id.tv_money)
    TextView tv_money;

    private int mSelectedFATC;
    private int mBMSZ;

    private int mTotalMoney;
    private int mBetType;
    private String mBetCode;
    private int mMultiple;
    private String mIssue;
    private String mFinishTime;
    private String mBetList;

    private int mMyMoney;

    private String min_reward,max_reward;


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_match_hemai;
    }

    @Override
    protected void initView() {
        mTotalMoney = getIntent().getIntExtra("total_money", 2);
        mBetType = getIntent().getIntExtra("bet_type", 2);
        mBetCode = getIntent().getStringExtra("bet_code");
        mBetList = getIntent().getStringExtra("bet_list");
        mMultiple = getIntent().getIntExtra("multiple", 1);
        mIssue = getIntent().getStringExtra("issue");
        mFinishTime = getIntent().getStringExtra("finish_time");

        min_reward = this.getIntent().getStringExtra("min_reward");
        max_reward = this.getIntent().getStringExtra("max_reward");

        tv_fangan_money.setText("方案金额：" + mTotalMoney + "元");
        mSelectedFATC = 4;
        mBMSZ = 2;
        resetFTBtns();
        resetMMSZ(tv_canyukejian, tv_gongkai, tv_jianzhikejian);
        check_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                ll_fangan_ticheng.setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });
        et_xuanyan.setText("竞彩足球合买");
        initEditText();
        resetMoney(1);
    }

    private void initEditText() {
        et_fenchengshu.setText(String.valueOf(mTotalMoney));
        et_fenchengshu.setSelection(String.valueOf(mTotalMoney).length());
        et_fenchengshu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String text = charSequence.toString();
                if (TextUtils.isEmpty(text)) {
                    T.showShort("分成份数不能为空");
                    tv_invalid_fenshu.setVisibility(View.VISIBLE);
                    tv_per_money.setText("每份：#元");
                    resetMoney(0);
                } else {
                    if (text.startsWith("0")) {
                        if (text.length() > 1) {
                            et_fenchengshu.setText("0");
                            T.showShort("请输入正确的数字格式");
                        } else {
                            T.showShort("份数不能为0");
                        }
                        tv_invalid_fenshu.setVisibility(View.VISIBLE);
                        tv_per_money.setText("每份：#元");
                        resetMoney(0);
                        return;
                    }
                    int fenshu = Integer.parseInt(text);
                    if (mTotalMoney % fenshu != 0) {
                        tv_invalid_fenshu.setVisibility(View.VISIBLE);
                        tv_per_money.setText("每份：#元");
                        T.showShort("每份金额需能整除，且至少1元");
                        resetMoney(0);
                    } else {
                        tv_invalid_fenshu.setVisibility(View.GONE);
                        tv_per_money.setText("每份：" + (mTotalMoney / fenshu) + "元");

                        String text2 = et_rengoushu.getText().toString();
                        int rengouFenshu = 0;
                        if (!TextUtils.isEmpty(text2)) {
                            rengouFenshu = Integer.parseInt(text2);
                        }
                        if (rengouFenshu <= 0) {
                            resetMoney(0);
                            return;
                        }
                        String text3 = et_baodishu.getText().toString();
                        int baodishu = 0;
                        if (!TextUtils.isEmpty(text3)) {
                            baodishu = Integer.parseInt(text3);
                        }
                        if (rengouFenshu + baodishu > fenshu) {
                            T.showShort("(认购份数+保底份数)需小于或等于总份数");
                            resetMoney(0);
                        } else {
                            resetMoney((rengouFenshu + baodishu) * (mTotalMoney / fenshu));
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        et_rengoushu.setText("1");
        et_rengoushu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String text = charSequence.toString();
                if (TextUtils.isEmpty(text)) {
                    T.showShort("认购份数不能为空");
                    resetMoney(0);
                } else {
                    if (text.startsWith("0")) {
                        et_rengoushu.setText("0");
                        et_rengoushu.setSelection(1);
                        T.showShort("认购份数需大于0");
                        resetMoney(0);
                        return;
                    }
                    int rengouFenshu = Integer.parseInt(text);
                    String text2 = et_baodishu.getText().toString();
                    int baodishu = 0;
                    if (!TextUtils.isEmpty(text2)) {
                        baodishu = Integer.parseInt(text2);
                    }
                    String text3 = et_fenchengshu.getText().toString();
                    int zongFenshu = 0;
                    if (!TextUtils.isEmpty(text3)) {
                        zongFenshu = Integer.parseInt(text3);
                    }
                    if (rengouFenshu + baodishu > zongFenshu) {
                        T.showShort("(认购份数+保底份数)需小于或等于总份数");
                        et_rengoushu.setText("1");
                        et_rengoushu.setSelection(1);
                        rengouFenshu = 1;
                    }
                    if (rengouFenshu + baodishu > zongFenshu) {
                        et_baodishu.setText("0");
                        baodishu = 1;
                    }
                    resetMoney((rengouFenshu + baodishu) * (mTotalMoney / zongFenshu));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        et_baodishu.setText("0");
        et_baodishu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String text = charSequence.toString();
                if (TextUtils.isEmpty(text)) {
                    T.showShort("保底份数不能为空");
                    resetMoney(0);
                } else {
                    if (text.startsWith("0") && text.length() > 1) {
                        et_baodishu.setText("0");
                        et_baodishu.setSelection(1);
                        T.showShort("请输入正确的数字格式");
                        resetMoney(0);
                        return;
                    }
                    int baodishu = Integer.parseInt(text);
                    String text2 = et_rengoushu.getText().toString();
                    int rengouFenshu = 0;
                    if (!TextUtils.isEmpty(text2)) {
                        rengouFenshu = Integer.parseInt(text2);
                    }
                    String text3 = et_fenchengshu.getText().toString();
                    int zongFenshu = 0;
                    if (!TextUtils.isEmpty(text3)) {
                        zongFenshu = Integer.parseInt(text3);
                    }
                    if (rengouFenshu + baodishu > zongFenshu) {
                        T.showShort("(认购份数+保底份数)需小于或等于总份数");
                        et_baodishu.setText("0");
                        et_baodishu.setSelection(1);
                        baodishu = 0;
                    }
                    if (rengouFenshu + baodishu > zongFenshu) {
                        T.showShort("(认购份数+保底份数)需小于或等于总份数");
                        et_rengoushu.setText("1");
                        rengouFenshu = 1;
                    }
                    resetMoney((rengouFenshu + baodishu) * (mTotalMoney / zongFenshu));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    private void resetMoney(int money) {
        mMyMoney = money;
        tv_money.setText(Html.fromHtml("支付金额：<font color=\"#D83220\">" + (money > 0 ? String.valueOf(money) : "#") + "</font>元"));
    }

    private void resetFTBtns() {
        for (int i = 0; i < ll_fangan_ticheng.getChildCount(); ++i) {
            LinearLayout ll = (LinearLayout) ll_fangan_ticheng.getChildAt(i);
            View view;
            for (int j = 0; j < ll.getChildCount(); ++j) {
                view = ll.getChildAt(j);
                view.setBackgroundResource(String.valueOf(mSelectedFATC).equals(view.getTag()) ?
                        R.drawable.bg_match_filter : R.drawable.bg_match_not_filter);
                ((TextView) view).setTextColor(getResources().getColor(String.valueOf(mSelectedFATC).equals(view.getTag()) ?
                        R.color.primary_comparison : R.color.text_primary));
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mSelectedFATC = Integer.parseInt((String) view.getTag());
                        resetFTBtns();
                    }
                });
            }
        }
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.img_back, R.id.img_question, R.id.tv_gongkai,
            R.id.tv_ok, R.id.tv_canyukejian, R.id.tv_jianzhikejian})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_question:
                break;
            case R.id.tv_ok:
                String text = et_fenchengshu.getText().toString();
                int zongFenshu = 0;
                if (TextUtils.isEmpty(text)) {
                    T.showShort("分成份数不能为空");
                    return;
                }
                if (text.startsWith("0")) {
                    T.showShort("分成份数不能为0");
                    return;
                }
                zongFenshu = Integer.parseInt(text);
                if (mTotalMoney % zongFenshu != 0) {
                    T.showShort("每份金额需能整除，且至少1元");
                    return;
                }

                String text2 = et_rengoushu.getText().toString();
                int rengouFenshu = 0;
                if (TextUtils.isEmpty(text2)) {
                    T.showShort("认购份数不能为空");
                    return;
                }
                if (text2.startsWith("0")) {
                    T.showShort("认购份数需大于0");
                    return;
                }
                rengouFenshu = Integer.parseInt(text2);

                String text3 = et_baodishu.getText().toString();
                if (TextUtils.isEmpty(text3)) {
                    T.showShort("保底份数不能为空");
                    return;
                }
                int baodishu = Integer.parseInt(text3);
                if (rengouFenshu + baodishu > zongFenshu) {
                    T.showShort("(认购份数+保底份数)需小于或等于总份数");
                    return;
                }
                resetMoney((rengouFenshu + baodishu) * (mTotalMoney / zongFenshu));

                String declaration = et_xuanyan.getText().toString();
                if (TextUtils.isEmpty(declaration)) {
                    T.showShort("宣言不能为空！");
                    return;
                }

                String desc = et_miaoshu.getText().toString();
                if (TextUtils.isEmpty(desc)) {
                    T.showShort("方案描述不能为空！");
                    return;
                }
//                mPresenter.createCopurch(mBetType, mMultiple, zongFenshu, mBetCode, mIssue, String.valueOf(mTotalMoney), text,
//                        String.valueOf(mSelectedFATC * 1.0 / 100), text2, text3, declaration, desc, stringToDateStr(mFinishTime));

                Intent intent = new Intent(this, PayActivity.class);
                intent.putExtra("order_type", mBetType);
                intent.putExtra("multiple", mMultiple);
                intent.putExtra("mut_num", zongFenshu);
                intent.putExtra("data", mBetCode);
                intent.putExtra("bet_list", mBetList);
                intent.putExtra("issue", mIssue);
                intent.putExtra("total_amount", mTotalMoney);
                intent.putExtra("share", text);
                intent.putExtra("percentage", String.valueOf(mSelectedFATC * 1.0 / 100));
                intent.putExtra("subscribe", text2);
                intent.putExtra("guarantee", text3);
                intent.putExtra("declaration", declaration);
                intent.putExtra("desc", desc);
                intent.putExtra("my_money", mMyMoney);
                intent.putExtra("finish_time", stringToDateStr(mFinishTime));
                intent.putExtra("min_reward", min_reward);
                intent.putExtra("max_reward", max_reward);
                startActivity(intent);
                overridePendingTransition(R.anim.push_bottom_in, R.anim.push_buttom_out);
                break;
            case R.id.tv_gongkai:
                mBMSZ = 1;
                resetMMSZ(tv_gongkai, tv_canyukejian, tv_jianzhikejian);
                break;
            case R.id.tv_canyukejian:
                mBMSZ = 2;
                resetMMSZ(tv_canyukejian, tv_gongkai, tv_jianzhikejian);
                break;
            case R.id.tv_jianzhikejian:
                mBMSZ = 3;
                resetMMSZ(tv_jianzhikejian, tv_canyukejian, tv_gongkai);
                break;
        }
    }

    private String stringToDateStr(String str) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatter.parse(str);
            return String.valueOf(date.getTime());
        } catch (Exception e) {

        }
        return "0";
    }

    private void resetMMSZ(TextView... views) {
        for (int i = 0; i < views.length; ++i) {
            if (i == 0) {
                views[i].setBackgroundResource(R.drawable.bg_match_filter);
                views[i].setTextColor(getResources().getColor(R.color.primary_comparison));
            } else {
                views[i].setBackgroundResource(R.drawable.bg_match_not_filter);
                views[i].setTextColor(getResources().getColor(R.color.text_primary));
            }
        }
    }
}
